<?php

/**
 * venta actions.
 *
 * @package    administracion-ventas
 * @subpackage venta
 * @author     Your name here
 * @version    SVN: $Id: actions.class.php 12474 2008-10-31 10:41:27Z fabien $
 */
class ventaActions extends sfActions
{
    public function executeIndex(sfWebRequest $request)
    {
        if ($request->isXmlHttpRequest())
        {
            
            $a = explode('/', $request->getParameter('fecha'));
            $fecha = mktime(0,0,0,$a[1], $a[0], $a[2]);
            $this->fecha_desde = date('Y-m-d', $fecha);
            $fecha = mktime(23,0,0,$a[1], $a[0], $a[2]);
            $this->fecha_hasta = date('Y-m-d H:i:s', $fecha);
        }
        else
        {
            $this->fecha_desde = date('Y-m-d H:i:s', mktime(1, 0, 0, date("m") , date("d"), date("Y")));
            $this->fecha_hasta = date('Y-m-d H:i:s', mktime(23, 0, 0, date("m") , date("d"), date("Y")));
        }

        $crit = new Criteria();
        $crit->add(VentaPeer::CREATED_AT, $this->fecha_desde, Criteria::GREATER_THAN);
        $crit->addAnd(VentaPeer::CREATED_AT, $this->fecha_hasta, Criteria::LESS_THAN);
        
        $this->linea_venta_list = LineaVentaPeer::doSelectJoinAll($crit);

        $crit2 = new Criteria();
        $crit2->add(EgresoPeer::CREATED_AT, $this->fecha_hasta, Criteria::LESS_EQUAL);
        $crit2->addAnd(EgresoPeer::CREATED_AT, $this->fecha_desde, Criteria::GREATER_EQUAL);

        $this->egresos = EgresoPeer::doSelect($crit2);

        if ($request->isXmlHttpRequest())
        return $this->renderPartial('linea_venta', array('linea_venta_list' => $this->linea_venta_list, 'egresos' => $this->egresos,
                'fecha_desde' => $this->fecha_desde, 'fecha_hasta' => $this->fecha_hasta));
    }

    public function executeNew(sfWebRequest $request)
    {
        $this->form = new VentaForm(null, array('url' => $this->getController()->genUrl('producto/ajax')));
    }

    public function executeCreate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod('post'));
        $removed_products = explode('-', $request->getParameter('removed_products'));
        if (in_array("", $removed_products)) $nro = $request->getParameter('nro_product'); else $nro = $request->getParameter('nro_product') + count($removed_products);
        $this->form = new VentaForm(null, array('nro' => $nro, 'removed_products' => $removed_products));

        $emb = $this->form->getEmbeddedForms();

        $this->processForm($request, $this->form);

        $this->setTemplate('new');
    }

    public function executeEdit(sfWebRequest $request)
    {
        $this->forward404Unless($venta = VentaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object venta does not exist (%s).', $request->getParameter('id')));
        $this->form = new VentaForm($venta);
    }

    public function executeUpdate(sfWebRequest $request)
    {
        $this->forward404Unless($request->isMethod('post') || $request->isMethod('put'));
        $this->forward404Unless($venta = VentaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object venta does not exist (%s).', $request->getParameter('id')));
        $this->form = new VentaForm($venta);

        $this->processForm($request, $this->form);

        $this->setTemplate('edit');
    }

    public function executeDelete(sfWebRequest $request)
    {
        $request->checkCSRFProtection();

        $this->forward404Unless($venta = VentaPeer::retrieveByPk($request->getParameter('id')), sprintf('Object venta does not exist (%s).', $request->getParameter('id')));
        $venta->delete();

        $this->redirect('venta/index');
    }

    protected function processForm(sfWebRequest $request, sfForm $form)
    {
        $form->bind($request->getParameter($form->getName()), $request->getFiles($form->getName()));

        if ($form->isValid())
        {

            $venta = $form->save();

            $this->redirect('venta/new');
        }

    }

    function executeAddProductAjax(sfWebRequest $request)
    {
        $this->nro = $nro = $request->getParameter('nro');
        $this->form = new VentaForm(null, array('nro' => $nro+1 ,'url' => $this->getController()->genUrl('producto/ajax')));
        return $this->renderPartial('product_row', array('form' => $this->form, 'nro' => $this->nro));
    }

    public function executeDelProductAjax(sfWebRequest $request)
    {
        $index = $request->getParameter('index');
        $nro = $request->getParameter('total_filas');
        $this->form = new VentaForm(null, array('nro' => ($nro-1) ,'url' => $this->getController()->genUrl('producto/ajax')));
        return $this->renderText('');
    }
}
